package com.company.cloud.user.modules.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.company.modules.api.request.ThirdPlatformConfigGetRequest;
import com.company.cloud.company.modules.api.response.ThirdPlatformConfigModel;
import com.company.cloud.company.modules.api.service.CompanyCheckRemoteService;
import com.company.cloud.company.modules.api.service.CompanyConfigRemoteService;
import com.company.cloud.user.modules.app.service.UserCompanyService;
import com.company.cloud.user.modules.cache.models.UserShareCodeModel;
import com.company.cloud.user.modules.cache.service.UserShareCodeCacheService;
import com.company.common.server.config.cloud.ProjectConfig;
import com.company.common.server.constant.LoginThirdChannelEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class UserCompanyServiceImpl implements UserCompanyService {

    @Qualifier("companyCheckRemoteServiceImpl")
    @Autowired
    private CompanyCheckRemoteService companyCheckRemoteService;

    @Qualifier("companyConfigRemoteServiceImpl")
    @Autowired
    private CompanyConfigRemoteService companyConfigRemoteService;

    @Autowired
    private ProjectConfig projectConfig;

    @Autowired
    private UserShareCodeCacheService userShareCodeCacheService;

    @Override
    public String companyCheck(String companyNo) {
        String companyNo2 = projectConfig.getCompanyNo(companyNo);
        if (StrUtil.isEmpty(companyNo2)) {
            throw new BusinessException(BaseReturnEnum.CompanyNo_NULL);
        }
//        BaseClientResult<String> companyCheck = companyCheckRemoteService.checkCompany(companyNo2);
//        CheckUtils.checkClientResult(companyCheck);
        return companyNo2;
    }

    @Override
    public String getCompany(String shareCode, String companyNo) {
        if (StrUtil.isNotEmpty(shareCode)) { //有推荐码
            UserShareCodeModel uscm = userShareCodeCacheService.getUserByShareCode(AuthContextUtils.getShareCode());
            if (uscm != null) {
                return uscm.getCompanyNo();
            }
        }
        //无推荐码
        String companyNo2 = projectConfig.getCompanyNo(companyNo);
        if (StrUtil.isEmpty(companyNo2)) {
            throw new BusinessException(BaseReturnEnum.CompanyNo_NULL);
        }
        return companyNo2;
    }

    @Override
    public String getCompany2Check(String shareCode, String companyNo) {
        String _companyNo = this.getCompany(shareCode, companyNo);
        this.companyCheck(_companyNo);
        return _companyNo;
    }

    @Override
    public ThirdPlatformConfigModel getThirdConfig(String companyNo, LoginThirdChannelEnum channel) {
        ThirdPlatformConfigGetRequest params = new ThirdPlatformConfigGetRequest();
        params.setCompanyNo(companyNo);
        params.setType(channel.getCode());
        BaseClientResult<ThirdPlatformConfigModel> result = companyConfigRemoteService.getThirdPlatformConfig(params);
        ThirdPlatformConfigModel data = BaseClientResult.getResultData2Error(result);
        return data;
    }
}
